#include "wuSolidCircleManager.h"

wuSolidCircleManager::wuSolidCircleManager()
{

}

wuSolidCircleManager::~wuSolidCircleManager()
{
}

bool wuSolidCircleManager::capture(ofImage *currImg, ofImage *bgImg, int x_offset)
{
	bool bCambiazo = true;

	wuBaseManager::capture(currImg, bgImg, x_offset);

	return bCambiazo;
}

void wuSolidCircleManager::addBitxo(ofImage *_skin, ofRectangle rect, vector <ofPoint> * _pts )
{
	wuSolidCircle circle;
	circle.setup(box2d, _skin, rect);
	circles.push_back(circle);
}

void wuSolidCircleManager::draw(bool bDrawlines, int offX)
{
        for (int i = 0; i < circles.size(); i++)
            circles[i].draw(bDrawlines, offX);
}

void wuSolidCircleManager::drawSelected(bool bDrawlines, int offX)
{

    if(selection.width < vw || selection.height < vh)
    {

        for (int i = circles.size()-1; i >= 0; i--)
        {
            bool condi = (circles[i].getPosition().y > selection.y) && (circles[i].getPosition().y < selection.y + selection.height) &&
             (circles[i].getPosition().x > selection.x + x_scroll) && (circles[i].getPosition().x < selection.x + x_scroll + selection.width);

            if (condi)
                circles[i].draw(bDrawlines, offX);
        }
    }else
    {
        for (int i = 0; i < circles.size(); i++)
            circles[i].draw(bDrawlines, offX);
    }
}

void wuSolidCircleManager::update()
{
        for (int i = 0; i < circles.size(); i++)
            circles[i].update();

		// destroy off-screen positions
		for (int i = circles.size()-1; i >= 0; i--)
        {
			if (circles[i].getPosition().y > vh)
				circles.erase(circles.begin()+i);
		}
}

void wuSolidCircleManager::clear()
{

    if(selection.width < vw || selection.height < vh)
    {

        for (int i = circles.size()-1; i >= 0; i--)
        {
            bool condi = (circles[i].getPosition().y > selection.y) && (circles[i].getPosition().y < selection.y + selection.height) &&
             (circles[i].getPosition().x > selection.x + x_scroll) && (circles[i].getPosition().x < selection.x + x_scroll + selection.width);

            if (condi)
            {
                circles[i].clear();
                circles.erase(circles.begin()+i);
            }
        }
    }else
    {
        for (int i = 0; i < circles.size(); i++)
            circles[i].clear();
        circles.clear();
    }
}

void wuSolidCircleManager::touch(int _x,int _y, int forceX, int forceY)
{
    for (int i = 0; i < circles.size(); i++)
		circles[i].touch(_x, _y, forceX, forceY);
}

bool wuSolidCircleManager::drag(int _x,int _y)
{
    bool bDrag=false;

    for (int i = 0; i < circles.size(); i++)
		if(circles[i].drag(_x + x_scroll, _y))
            bDrag=true;

    return bDrag;

}

vector <ofPoint> wuSolidCircleManager::getPosition()
{
    vector <ofPoint> positions;

    for (int i = 0; i < circles.size(); i++)
        positions.push_back(circles[i].getPosition());

    return positions;
}

void wuSolidCircleManager::setPosition(vector <ofPoint> positions)
{

    for (int i = 0; i < circles.size(); i++)
        circles[i].setPosition(positions[i]);
}

void wuSolidCircleManager::stop_gravity()
{

    for (int i = 0; i < circles.size(); i++)
        circles[i].stop_gravity();
}
